﻿using System.Collections;
using UnityEngine;

public class SceneBgMgr
{
    private Transform scene;
    private Transform tran;
    private UITexture sceneBg;
    private UITexture tranBg;

    private TweenColor tweenColor;
    private bool changeingBg;
    string currentPic = "null";

    public SceneBgMgr()
    {
        scene = AllUpdate.GetChildByName("SceneBg");
        tran = AllUpdate.GetChildByName("Transition");
        sceneBg = scene.GetComponentInChildren<UITexture>();
        tranBg = tran.GetComponentInChildren<UITexture>();
        tweenColor = tranBg.GetComponent<TweenColor>();

        CloseSceneBg(); CloseTranBg(); tweenColor.enabled = false;
        EventDelegate.Add(tweenColor.onFinished, CloseTranBg);
    }

    public void CloseSceneBg()
    {
        Utility.SetActive(scene, false);
    }

    private void CloseTranBg()
    {
        Utility.SetActive(tran, false);
    }

    public void ResetBgLayer()
    {
        //设置背景层layer为SceneBg
        scene.gameObject.layer = 10;
    }
    
    /// <summary>
    /// 切换2d背景 
    /// </summary>
    /// <param name="bgName">背景图片名称</param>
    /// <param name="isTran">是否淡入淡出</param>
    public void ChangeBG(string bgName, bool isTran = true, Texture texture = null)
    {
        Utility.SetActive(scene, true);
        BehaviourUtil.StartCoroutine(RunChangeBG(bgName, isTran, texture));
    }

    private IEnumerator RunChangeBG(string bgName, bool isTran, Texture _texture = null)
    {
        // 不是3d层，强制设置成3d层
        if (sceneBg.gameObject.layer != 10)
        {
            ResetBgLayer();
        }
        if (bgName == "no") { yield break; }

        while (true)
        {
            if (!changeingBg) { yield break; }
            yield return new WaitForFixedUpdate();
        }

        if (currentPic != bgName)
        {
            changeingBg = true;
            Texture texture = _texture;
            if (texture == null)
            {
                OnLoadArg arg = new OnLoadArg();
                string path = PathUtil.UITextureUrl + bgName + ".png.uab";
                yield return BehaviourUtil.StartCoroutine(ZMgr.ResourceMgr.CoroutineLoad(path, arg));
                texture = arg.asset as Texture;
            }

            if (isTran && currentPic != "null")
            {
                Texture currentTexture = null;
                OnLoadArg arg = new OnLoadArg();
                string path = PathUtil.UITextureUrl + currentPic + ".png.uab";
                yield return BehaviourUtil.StartCoroutine(ZMgr.ResourceMgr.CoroutineLoad(path, arg));
                currentTexture = arg.asset as Texture;
                
                SetUITexture(true, currentTexture);
                tweenColor.ResetToBeginning();
                tweenColor.enabled = true;
            }
            SetUITexture(false, texture);
            currentPic = bgName;
            changeingBg = false;
        }
    }

    public void SetUITexture(bool isTran, Texture texture)
    {
        if (isTran)
        {
            tranBg.mainTexture = texture;
            tranBg.width = GameTool.ScreenWidth;
            tranBg.height = GameTool.ScreenHeight;
            Utility.SetActive(tran, true);
        }
        else
        {
            sceneBg.mainTexture = texture;
            sceneBg.width = GameTool.ScreenWidth;
            sceneBg.height = GameTool.ScreenHeight;
            Utility.SetActive(scene, true);
        }
    }
}